﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Media;

namespace DoAnHuongRung
{
    public partial class frmLapDonDatHang : Form
    {
        public frmLapDonDatHang()
        {
            InitializeComponent();
        }

        private void frmLapDonDatHang_Load(object sender, EventArgs e)
        {
            lblNgayLap.Text = DateTime.Now.ToShortDateString();
            LoadCmbPhieuSuDungNVL();
            LoadDtgNhaCC();
            LoadDataTxtTimkiem();
        }

        private void LoadCmbPhieuSuDungNVL()
        {
            cmbPhieuSuDungNVL.ValueMember = "MaPhieuSuDungNVL";
            cmbPhieuSuDungNVL.DisplayMember = "MaPhieuSuDungNVL";
            cmbPhieuSuDungNVL.DataSource = PhieuSuDungNVLBUS.Select_Phieu_not_read_yet();
        }

        private void txtTimKiemNCC_Click(object sender, EventArgs e)
        {
            if (txtTimKiemNCC.Text.Equals("Tìm kiếm"))
            {
                txtTimKiemNCC.Text = "";
                txtTimKiemNCC.ForeColor = Color.Black;
            }
        }

        private void LoadDtgNhaCC()
        {
            dtgNhaCC.AutoGenerateColumns = false;
            dtgNhaCC.DataSource = NhaCungCapBUS.SelectAll();
            
        } 

        private void LoadDtgNVL(int MaNCC)
        {
            List<NguyenVatLieuDTO> listnvl= NguyenVatLieuBUS.Select_by_MaNCC(MaNCC);
            dtgNVL.AutoGenerateColumns = false;
            dtgNVL.DataSource = listnvl;
            for (int i = 0; i < listnvl.Count(); i++)
            {
                bool b = false;
                int vitri = -1;
                for (int j = 0; j < listChiTietDatHang.Count(); j++)
                {
                    if (listnvl[i].ID_NCC_NVL == listChiTietDatHang[j].ID_NCC_NVL)
                    {
                        vitri = j;
                        b = true;
                        break;
                    }
                }
                if (b == true)
                {
                    dtgNVL.Rows[i].Cells[0].Value = "True";
                    dtgNVL.Rows[i].Cells[3].Value = listChiTietDatHang[vitri].SoLuong.ToString();
                }
                else
                    dtgNVL.Rows[i].Cells[3].Value = "10";
            }
        }

        private void dtgNhaCC_SelectionChanged(object sender, EventArgs e)
        {
            string MaNCC = dtgNhaCC.CurrentRow.Cells[0].Value.ToString();
            LoadDtgNVL(int.Parse(MaNCC));
        }

        List<ChiTietDatHang> listChiTietDatHang=new List<ChiTietDatHang>();

        private void dtgNVL_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (((System.Windows.Forms.DataGridView)(sender)).Rows[e.RowIndex].Cells[e.ColumnIndex].ColumnIndex == 0)
            {
                string flag = dtgNVL.CurrentRow.Cells[0].EditedFormattedValue.ToString();
                int SoLuong = int.Parse(dtgNVL.CurrentRow.Cells[3].Value.ToString());
                int MaNCC = int.Parse(dtgNhaCC.CurrentRow.Cells[0].Value.ToString());
                int ID_NCC_NVL = int.Parse(dtgNVL.CurrentRow.Cells[4].Value.ToString());
                int MaNVL = int.Parse(dtgNVL.CurrentRow.Cells[5].Value.ToString());
                string DonGia = dtgNVL.CurrentRow.Cells[1].Value.ToString();
                string[] arr = DonGia.Split('.');
                DonGia = "";
                for (int i = 0; i < arr.Length; i++)
                {
                    DonGia += arr[i];
                }

                ChiTietDatHang ctdh = new ChiTietDatHang();
                ctdh.MaNCC = MaNCC;
                ctdh.ID_NCC_NVL = ID_NCC_NVL;
                ctdh.SoLuong = SoLuong;
                ctdh.DonGia = int.Parse(DonGia);
                ctdh.MaNVL = MaNVL;
                ctdh.DonViTinh = dtgNVL.CurrentRow.Cells[6].Value.ToString();
                if (flag.Equals("True"))
                {
                    listChiTietDatHang.Add(ctdh);
                }
                else
                {
                    int vitri = -1;
                    for (int i = 0; i < listChiTietDatHang.Count(); i++)
                    {
                        if (listChiTietDatHang[i].ID_NCC_NVL == ctdh.ID_NCC_NVL)
                        {
                            vitri = i;
                            break;
                        }
                    }
                    if (vitri != -1)
                        listChiTietDatHang.RemoveAt(vitri);
                }

                //Tính giá trị hóa đơn 
                int iTriGia = 0;
                for (int i = 0; i < listChiTietDatHang.Count(); i++)
                {
                    iTriGia += listChiTietDatHang[i].SoLuong * listChiTietDatHang[i].DonGia;
                }
                string sTriGia = "";
                string tmp = iTriGia.ToString();
                while (tmp.Length > 3)
                {
                    sTriGia += "." + tmp.Substring(tmp.Length - 3);
                    tmp = tmp.Substring(0, tmp.Length - 3);
                }
                lblTriGia.Text = tmp + sTriGia + " VNĐ";
                //lblTriGia.Text = iTriGia.ToString();
            }          
        }

        private void LoadDataTxtTimkiem()
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            List<NhaCungCapDTO> list = NhaCungCapBUS.SelectAll();
            for (int i = 0; i < list.Count(); i++)
                auto.Add(list[i].TenNCC);
            txtTimKiemNCC.AutoCompleteCustomSource = auto;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keydata)
        {
            if (keydata == Keys.Enter)
            {
                if (txtTimKiemNCC.Focused == true && !txtTimKiemNCC.Text.Equals(""))
                {
                    dtgNhaCC.DataSource = NhaCungCapBUS.Search_by_TenNCC(txtTimKiemNCC.Text);
                }
            }
            return false;
        }

        private class ChiTietDatHang
        {
            private int _MaNCC;

            public int MaNCC
            {
                get { return _MaNCC; }
                set { _MaNCC = value; }
            }
            private int _ID_NCC_NVL;

            public int ID_NCC_NVL
            {
                get { return _ID_NCC_NVL; }
                set { _ID_NCC_NVL = value; }
            }
            private int _SoLuong;

            public int SoLuong
            {
                get { return _SoLuong; }
                set { _SoLuong = value; }
            }

            private int _DonGia;

            public int DonGia
            {
                get { return _DonGia; }
                set { _DonGia = value; }
            }

            private int _MaNVL;

            public int MaNVL
            {
                get { return _MaNVL; }
                set { _MaNVL = value; }
            }

            private string _DonViTinh;

            public string DonViTinh
            {
                get { return _DonViTinh; }
                set { _DonViTinh = value; }
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {                
            LoadCmbPhieuSuDungNVL();
            LoadDtgNhaCC();
            txtTimKiemNCC.Text = "Tìm kiếm";
            txtTimKiemNCC.ForeColor = Color.Gray;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                List<int> listNCC = new List<int>();
                listNCC.Add(listChiTietDatHang[0].MaNCC);
                int i;
                bool b = false;
                for (i = 1; i < listChiTietDatHang.Count(); i++)
                {
                    int j = 0;
                    b = false;
                    for (j = 0; j < listNCC.Count(); j++)
                    {
                        if (listNCC[j] == listChiTietDatHang[i].MaNCC)
                        {
                            b = true;
                            break;
                        }
                    }
                    if (b == false)
                        listNCC.Add(listChiTietDatHang[j].MaNCC);
                }
                //Thêm DDH
                b = false;
                for (i = 0; i < listNCC.Count(); i++)
                {
                    DonDatHangDTO ddh = new DonDatHangDTO();
                    ddh.NguoiLap = 2;
                    ddh.NgayLap = DateTime.Now.ToShortDateString();
                    ddh.PhieuSuDungNVL = int.Parse(cmbPhieuSuDungNVL.SelectedValue.ToString());
                    ddh.MaNCC = listNCC[i];
                    ddh.TriGia = "0";
                    b = DonDatHangBUS.Insert(ddh);
                    if (b == true)
                    {
                        int MaDDH = DonDatHangBUS.Select_MaDDH_Max();
                        int TongTien = 0;
                        for (int j = 0; j < listChiTietDatHang.Count(); j++)
                        {
                            if (listChiTietDatHang[j].MaNCC == listNCC[i])
                            {
                                TongTien += listChiTietDatHang[j].SoLuong * listChiTietDatHang[j].DonGia;
                                ChiTietDonDatHangDTO ctddh = new ChiTietDonDatHangDTO();
                                ctddh.MaDDH = MaDDH;
                                ctddh.MaNVL = listChiTietDatHang[j].MaNVL;
                                ctddh.DonViTinh = listChiTietDatHang[j].DonViTinh;
                                ctddh.SoLuong = listChiTietDatHang[j].SoLuong;
                                ctddh.ThanhTien = (listChiTietDatHang[j].SoLuong * listChiTietDatHang[j].DonGia).ToString();
                                ChiTietDonDatHangBUS.Insert(ctddh);
                            }
                        }
                        ddh.MaDDH = MaDDH;
                        ddh.TriGia = TongTien.ToString();
                        DonDatHangBUS.Update(ddh);
                    }
                }
                if (b == true)
                {
                    listChiTietDatHang = new List<ChiTietDatHang>();
                    PhieuSuDungNVLBUS.Update_TinhTrang(cmbPhieuSuDungNVL.SelectedValue.ToString(), 1);
                    LoadCmbPhieuSuDungNVL();
                    MessageBox.Show("Lập đơn đặt hàng thành công !!!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXoaChiTietDDH_Click(object sender, EventArgs e)
        {
            listChiTietDatHang = new List<ChiTietDatHang>();
            lblTriGia.Text = "0 VNĐ";
            LoadDtgNhaCC();
        }
    }
}
